<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    test
    <script>
      var notempty = function (arrObj) {
        var arr = [];
        map((item) => {
          if (item.val !== "" && val != undefined) {
            arr.push(item);
          }
        });

        return arr;
      };

      let toTimeStap = function (time) {
        return Date.parse(new Date(time).toString());
      };

      var arr = [
        {
          Exam_ExamDataOcr: [
            {
              ExamItemID: "54",
              ExamItemName: "视野检查（中心视野）",
              ExamInfo_ID:
                "1.2.276.0.7230010.3.1.2.22095313475612.2684.20220811023116.1",
              ExamBeginDate: "2022-05-29T12:14:37",
              DataOcrValueList: [
                {
                  ExamInfo_ID:
                    "1.2.276.0.7230010.3.1.2.22095313475612.2684.20220811023116.1",
                  Eyes: "OD",
                  CureName: "MD",
                  StandardName: "11",
                  MaxValue: "1.4",
                  MinValue: "0.1",
                  DataOcrVaule: "-0.19",
                },
                {
                  ExamInfo_ID:
                    "1.2.276.0.7230010.3.1.2.22095313475612.2684.20220811023116.1",
                  Eyes: "OD",
                  CureName: "PSD",
                  StandardName: "11",
                  MaxValue: "1.4",
                  MinValue: "0.1",
                  DataOcrVaule: "1.15",
                },
              ],
            },
            {
              ExamItemID: "54",
              ExamItemName: "视野检查（中心视野）",
              ExamInfo_ID:
                "1.2.276.0.7230010.3.1.2.22095313475612.2684.20220811022304.1",
              ExamBeginDate: "2022-05-25T14:52:28",
              DataOcrValueList: [
                {
                  ExamInfo_ID:
                    "1.2.276.0.7230010.3.1.2.22095313475612.2684.20220811022304.1",
                  Eyes: "OD",
                  CureName: "MD",
                  StandardName: "11",
                  MaxValue: "1.4",
                  MinValue: "0.1",
                  DataOcrVaule: "0.14",
                },
                {
                  ExamInfo_ID:
                    "1.2.276.0.7230010.3.1.2.22095313475612.2684.20220811022304.1",
                  Eyes: "OD",
                  CureName: "PSD",
                  StandardName: "11",
                  MaxValue: "1.4",
                  MinValue: "0.1",
                  DataOcrVaule: "1.22",
                },
                {
                  ExamInfo_ID:
                    "1.2.276.0.7230010.3.1.2.22095313475612.2684.20220811022304.1",
                  Eyes: "OS",
                  CureName: "MD",
                  StandardName: "11",
                  MaxValue: "1.4",
                  MinValue: "0.1",
                  DataOcrVaule: "-0.4",
                },
                {
                  ExamInfo_ID:
                    "1.2.276.0.7230010.3.1.2.22095313475612.2684.20220811022304.1",
                  Eyes: "OS",
                  CureName: "PSD",
                  StandardName: "11",
                  MaxValue: "1.4",
                  MinValue: "0.1",
                  DataOcrVaule: "1.26",
                },
              ],
            },
            {
              ExamItemID: "54",
              ExamItemName: "视野检查（中心视野）",
              ExamInfo_ID:
                "1.2.276.0.7230010.3.1.2.22095313475612.2684.20220811021320.1",
              ExamBeginDate: "2022-04-24T12:14:37",
              DataOcrValueList: [
                {
                  ExamInfo_ID:
                    "1.2.276.0.7230010.3.1.2.22095313475612.2684.20220811021320.1",
                  Eyes: "OS",
                  CureName: "MD",
                  StandardName: "11",
                  MaxValue: "1.4",
                  MinValue: "0.1",
                  DataOcrVaule: "-0.82",
                },
                {
                  ExamInfo_ID:
                    "1.2.276.0.7230010.3.1.2.22095313475612.2684.20220811021320.1",
                  Eyes: "OS",
                  CureName: "PSD",
                  StandardName: "11",
                  MaxValue: "1.4",
                  MinValue: "0.1",
                  DataOcrVaule: "1.49",
                },
              ],
            },
          ],
          Exam_ExamItemID: "'54'",
        },
      ];

      var arr0 = arr[0];
      var Exam_ExamDataOcr = arr0.Exam_ExamDataOcr;

      // Date 是x 上的点， CureName+Eyes 是类别，y 上点事DataOcrValue 一种类型对应一条线。

      //  x 点提取，一层遍历Exam_ExamDataOcr，读取 ExamBeginDate 字段写入 []
      // y 点提取，Exam_ExamDataOcr[i].DataOcrValueList[j].CureName & DataOcrVaule；相同CureName的放到一个数据里。

      var series = [];
      var xDots = [];
      let namesList = [];
      let dotValList = [];
      let timeDotDict = [];

      for (let x = 0; x < Exam_ExamDataOcr.length; x++) {
        let itemx = Exam_ExamDataOcr[x];
        xDots.push(toTimeStap(itemx.ExamBeginDate));

        for (let y = 0; y < itemx.DataOcrValueList.length; y++) {
          let itemy = itemx.DataOcrValueList[y];
          namesList.push(itemy.CureName + itemy.Eyes);
          dotValList.push({
            [itemy.CureName + itemy.Eyes]: itemy.DataOcrVaule,
            time: toTimeStap(itemx.ExamBeginDate), //time 节点
          });

          timeDotDict.push({
            ExamBeginDate: itemx.ExamBeginDate,
            DataOcrVaule: itemy.DataOcrVaule,
          });
        }
      }
      //类别去重保证唯一性
      let uniNameList = Array.from(new Set(namesList));
      console.log(timeDotDict, "timeDotDict");

      //   timeDotDict.forEach(item=>{
      //     item.ExamBeginDate = toTimeStap(item.ExamBeginDate)
      //   })

      //按照类别分发散点值
      console.log(
        uniNameList,
        { dotValList },
        "uniNameList",
        timeDotDict,
        "timeDotDict"
      );

      for (let z = 0; z < uniNameList.length; z++) {
        let itemz = uniNameList[z];
        let temp = [];

        for (let i = 0; i < dotValList.length; i++) {
          let itemi = dotValList[i];

          temp.push({ val: itemi[itemz], time: itemi.time });
        }

        series.push({
          name: itemz,
          type: "line",
          data: temp,
        });
      }

      series.forEach((item) => {
        var arr = [];
        for (let k = 0; k < item.data.length; k++) {
          if (item.data[k].val !== "" && item.data[k].val != undefined) {
            arr.push(item.data[k]);
          }
        }

        item.data = arr;
      });
      console.log(series, "series", xDots, "x");

      let allIndex = [];
      let xDotsLen = xDots.length;

      series.forEach((itemj, indexJ) => {
        let hasIndex = [];

        itemj.data.forEach((itemz, indexZ) => {
          xDots.forEach((itemi, indexI) => {
            if (itemz.time == itemi) {
              hasIndex.push(indexI);
            }
          });
        });

        allIndex.push(hasIndex);
      });

      // find no has index
      // 如果是单点缺失
      var allNoIndex = [];
      allIndex.forEach((item) => {
        let noHasIndex = undefined;
        if (xDotsLen !== item.length) {
          for (let i = 0; i < xDotsLen; i++) {
            if (item.includes(i) === false) {
              noHasIndex = i;
            }
          }
        }
        allNoIndex.push(noHasIndex);
      });

      // 如果是多个点缺失
      //   var allNoIndex = [];
      //   allIndex.forEach((item) => {
      //     let noHasIndex = [];
      //     if (xDotsLen !== item.length) {
      //       for (let i = 0; i < xDotsLen; i++) {
      //         if (item.includes(i) === false) {
      //           noHasIndex.push(i);
      //         }
      //       }
      //     }
      //     allNoIndex.push(noHasIndex);
      //   });

      console.log(allNoIndex, "allNoIndex");
      //insert allNoIndex  [2,2,0,0] =>  [[2],[2],[0],[0]]   [[2,0],[3],[1],[0,1]]

      series.forEach((item, index) => {
        console.log(allNoIndex[index], "allIndex[index]");

        // item.data.forEach((itemi, indexI) => {
        item.data.splice([allNoIndex[index]], 0, {
          val: "",
          time: xDots[allNoIndex[index]],
        });
        // });
      });

      console.log(series, "series");
      // last handle
      let result = [];
      series.forEach((item, index) => {
        let temp = [];
        item.data.forEach((itemi, indexI) => {
          temp.push(itemi.val);
        });
        result.push({
          name: item.name,
          type: "line",
          data: temp,
        });
      });

      console.log(series, "last-series", result);
    </script>
  </body>
</html>
